<% layout('/layouts/default.html', {title: '单项指标', libs: ['layout','zTree','dataGrid']}){ %>
<style>
    .lehand_title{
        line-height: 50px;
        text-align: center;
        font-size: 16px;
        color: #000000;
        margin-top: -10px;
        border-bottom: 1px solid #cccccc;
    }
    .lehand_title_ul{
        width: 100%;
        text-align: left;
    }
    .lehand_title_ul a{
        display: block;
        margin:0px;
        list-style: none;
        font-size: 14px;
        line-height: 35px;
        color: #000000;
        cursor: pointer;
    }
    .lehand_title_ul a:hover{
        color: #3c8dbc;
    }

    .lehand_title a{
        color: #000000;
        cursor: pointer;
    }
    .lehand_title a:hover{
        color: #3c8dbc;
    }
    .activeTi{
        color: #3c8dbc !important;
    }
    .btn-group-sm>.btn, .btn-sm {
        padding: 6px 12px;
        font-size: 14px;
        line-height: 1.3;
        height: 40px;
        margin-left: 20px;
        margin-top: 5px;
    }
    .ui-layout-pane-north {
        height: 50px;
        position: absolute;
        margin: 0px;
        inset: 0px 0px auto;
        width: auto;
        z-index: 0;
        display: block;
        visibility: visible;
    }
    .ui-content .content-open {
        display: none;
    }

    .ui-layout-toggler .ui-content .fa {
        line-height: 8px;
        display: none;
    }
    .ui-jqgrid {
        animation-duration: .5s;
        animation-name: fadeIn;
        height: 600px;
    }
    .ui-layout-content {
        padding: 10px;
        position: relative;
        overflow: hidden;
        width: 100%;
        border: 0;
    }
    .btu1 {
        margin: 7px 7px;
        text-align: center;
        display: inline-flex;
        background: #fff;
    }

    .btu2 {
        display: inline-flex;
        float: right;
        overflow: no-display;
        margin: 7px 7px;
    }
</style>
<div class="ui-layout-north" style="height: 50px">
    <div class="btu1">
        <button class="btn btn-default btn-sm" style="font-size: 16px" onclick="js.closeCurrentTabPage()" >返回</button>
    </div>
    <div class="btu2">
        <button class="btn btn-primary btn-sm" style="font-size: 16px" onclick="exportFile()">导出</button>
    </div>
</div>
<div class="ui-layout-west">
    <div class="main-content">
        <div class="box box-main">
            <div class="box-header" style="padding-bottom: 4px;">
                <div class="box-title">
                    <i class="fa icon-grid"></i> ${text('考核自评查看')}
                </div>
            </div>
            <div class="box-body">
                <#form:form id="searchForm" action="${ctx}/exam/dcSupExamSumScoreResult/unitList" method="post" class="form-inline"
                data-page-no="${parameter.pageNo}" data-page-size="${parameter.pageSize}"
                data-order-by="${parameter.orderBy}">
                <div class="input-group">
                    <#form:input path="taskDesc" id="unitName" maxlength="200" class="form-control" oninput="request()"
                    style="width:205px;" placeholder="请输入被考核单位名称" autocomplete="off"/>
                </div>
             </#form:form>
            </div>
            <div class="ui-layout-content">
                <table id="leftDataGrid"></table>
            </div>
        </div>
    </div>
</div>
<div class="ui-layout-center">
    <iframe id="mainFrame" name="mainFrame" class="ui-layout-content p0"></iframe>
</div>
<% } %>
<script>
    // 初始化布局
    $('body').layout({
        west__initClosed: $(window).width() <= 767, // 是否默认关闭
        west__size: 250
    });

    // 定义全局变量
    var examSumScore = ${toJson(examSumScore)};
    let gobal_flag = true;
    // 主页框架
    $(function () {
        //查询被考核单位
        left(examSumScore.examId, null);
    });

    // 输入框事件
    function request() {
        var info = $("#unitName").val();
        if (info == ''){
            return;
        }
        left(examSumScore.examId,info);
    }

    function left(examId, unitName) {
        js.ajaxSubmit({
            url: '${ctx}/exam/dcSupExamSumScoreResult/unitList',
            data: {'examId': examId, 'unitName': unitName},
            datatype: 'json',
            callback: function (res) {
                $("#leftDataGrid").dataGrid({
                    // 是否显示行号
                    showRownum: false,
                    data: res.page.list,
                    datatype: 'local',
                    columnModel: [
                        {
                            header: '${text("考核单位名称")}',
                            name: 'exameOrgName',
                            align: "center",
                            formatter: function (val, obj, row, act) {
                                return "<span onclick='dianji(" + row.exameOrg + ")'>" + row.exameOrgName + "</span>"
                            }
                        },
                        {header: 'id', name: 'exameOrg', hidden: true} // 不可删除
                    ],
                    onSelectRow: function(id, isSelect, event) {
                        let selectRow = $('#leftDataGrid').dataGrid('getRowData', id);
                        $('#mainFrame').attr('src', '${ctx}/exam/dcSupExamSumScoreResult/checkIndexData?examId='+examSumScore.examId+'&orgCode='+selectRow.exameOrg+'&year='+examSumScore.year);
                    },
                    ajaxSuccess:function (data){
                        if (data.total>0 && gobal_flag){
                            gobal_flag=false;
                            let row=data.rows[0];
                            $('#mainFrame').attr('src', '${ctx}/exam/dcSupExamSumScoreResult/checkIndexData?examId='+examSumScore.examId+'&orgCode='+row.exameOrg+'&year='+examSumScore.year);
                        }
                    }
                });
            },
            async: true,
            message: "正在获取数据.."
        })
    }



    function exportFile(){
        // 传递参数有  考核任务id 考核年份  考核机构代码
        let allData = $("#leftDataGrid").dataGrid('getDataIDs');
        if (allData == ''){
            js.showErrorMessage("暂无数据导出");
            return;
        }
        var orgCode='';
        for (let code of allData){
            orgCode =orgCode +( $('#leftDataGrid').dataGrid('getRowData', code).exameOrg)+",";
        }
        let examId=examSumScore.examId;
        let year= examSumScore.year;
        let xhr = new XMLHttpRequest();
        xhr.open('post', '${ctx}/exam/dcSupExamSumScoreResult/export?examId='+examId+'&year='+year+'&orgCode='+orgCode, true);
        xhr.setRequestHeader('Content-Type', `application/json`);
        xhr.responseType = 'blob';
        let that = this;
        let fileName="自评查看";
        xhr.onload = function () {
            if (this.status == 200) {
                //接受二进制文件流
                let blob = this.response;
                let downloadElement = document.createElement('a');
                let href = blob;
                if (typeof blob == 'string') {
                    downloadElement.target = '_blank';
                } else {
                    href = window.URL.createObjectURL(blob) ;//创建下载的链接
                }
                downloadElement.href = href;
                //下载后文件名
                downloadElement.download = fileName +".xlsx";
                downloadElement.click(); //点击下载
                document.body.removeChild(downloadElement); //下载完成移除元素
                if (typeof blob != 'string') {
                    window.URL.revokeObjectURL(href); //释放掉blob对象
                }
            }
        }
        xhr.send();
    }


</script>
